from peewee import *
from search_user_srv.config import config
from time import time


class BaseModel(Model):
    create_time = BigIntegerField(default=time() * 1000, null=False, verbose_name="创建时间")
    update_time = BigIntegerField(default=0, null=False, verbose_name="更新时间")
    del_time = IntegerField(default=0, null=False, verbose_name="删除时间")

    def save(self, *args, **kwargs):
        self.update_time = time() * 1000
        return super(BaseModel, self).save(*args, **kwargs)

    @classmethod
    def select(cls, *selection):
        # 在大多数情况下,此方法有效,但在某些情况下,如果使用带有关键字“ IN”的联接结束结果查询时出现错误,则选择它会中断：“ 1241,’操作数应包含1列”
        return super(BaseModel, cls).select(cls, *selection).where(cls.del_time == 0)

    def soft_delete(self):
        self.del_time = time()
        return super(BaseModel, self).save()


    class Meta:
        database = config.DB


STATUS_CHOICES = (
    (0, "禁用"),
    (1, "启用")
)


# 用户模型
class User(BaseModel):
    GENDER_CHOICES = (
        (0, "不限"),
        (1, "男"),
        (2, "女")
    )
    mobile = CharField(max_length=11, index=True, unique=True, verbose_name="手机号码")
    password = CharField(max_length=100, verbose_name="密码")
    safe_password = CharField(max_length=100, verbose_name="安全密码")
    nickname = CharField(max_length=20, default="", verbose_name="昵称")
    college = CharField(max_length=20, default="", verbose_name="所属系部")
    student_class = CharField(default="", verbose_name="学生班级")
    address = CharField(default="", verbose_name="用户地址")
    head_url = CharField(max_length=255, default="", verbose_name="用户头像")
    status = IntegerField(default=1, choices=STATUS_CHOICES, verbose_name="用户状态(0禁用,1启用)")
    birthday = DateField(null=True, verbose_name="出生日期")
    gender = IntegerField(choices=GENDER_CHOICES, default=0, verbose_name="性别(0不限,1男,2女)")
    orange_key = CharField(max_length=20, unique=True, null=False, verbose_name="我的推荐码")


# 后台管理员模型
class SysUser(BaseModel):
    username = CharField(max_length=20, index=True, unique=True, verbose_name="用户名")
    password = CharField(max_length=200, verbose_name="密码")
    nickname = CharField(max_length=20, verbose_name="昵称")
    head_url = CharField(max_length=255, verbose_name="头像")
    status = IntegerField(default=1, choices=STATUS_CHOICES, verbose_name="用户状态(0禁用,1启用)")
    salt = CharField(null=False, default="", verbose_name="盐")



# 用户实名认证
class UserVerify(BaseModel):
    uid = IntegerField(index=True, null=False, verbose_name="用户id")
    true_name = CharField(null=False, verbose_name="真实姓名")
    id_sn = CharField(null=False, verbose_name="身份证号")
    card_sn = CharField(null=False, verbose_name="学号")
    card_img = CharField(null=False, verbose_name="学生证照片")
    status = IntegerField(null=False, default=0, verbose_name="审核状态(0审核中 1审核通过 2审核失败)")
    reason = CharField(null=False, default="", verbose_name="审核原因")


if __name__ == '__main__':
    models = [User, SysUser, UserVerify]
    config.DB.create_tables(models)
    print("创建成功")
